#include "CreateCylinderWidget.h"

#include "Core/MainWindow.h"
#include "FeatureShape/FeatureShape.h"

CreateCylinderWidget::CreateCylinderWidget(MainWindow* inMainWindow, ShapeOperation* inOperation, QWidget* parent)
	: QRWCreateSolidWidget(inMainWindow, inOperation, parent)
	, ui(new Ui::CreateCylinderWidgetClass())
{
	ui->setupUi(this);

	connect(ui->confirm, &QPushButton::clicked, this, &CreateCylinderWidget::onConfirm);
	connect(ui->cancel, &QPushButton::clicked, this, &CreateCylinderWidget::close);
	connect(ui->modify, &QPushButton::clicked, this, &CreateCylinderWidget::onApply);

	connect(ui->radius, &QDoubleSpinBox::valueChanged, this, &CreateCylinderWidget::onSizeChanged);
	connect(ui->height, &QDoubleSpinBox::valueChanged, this, &CreateCylinderWidget::onSizeChanged);

	connect(ui->pX, &QDoubleSpinBox::valueChanged, this, &CreateCylinderWidget::onPosChanged);
	connect(ui->pY, &QDoubleSpinBox::valueChanged, this, &CreateCylinderWidget::onPosChanged);
	connect(ui->pZ, &QDoubleSpinBox::valueChanged, this, &CreateCylinderWidget::onPosChanged);

	connect(ui->selectCombo, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &CreateCylinderWidget::OnComboBoxIndexChanged);
	m_originPoint = new RWTripleFloatInput(ui->pX, ui->pY, ui->pZ);

	if (inOperation)
	{
		CreateShapeOperation* CreateOP = static_cast<CreateShapeOperation*>(inOperation);
		FeatureShapeCylinder* CylinderShape = static_cast<FeatureShapeCylinder*>(CreateOP->m_featureShape.get());
		ui->radius->setValue(CylinderShape->_radius);
		ui->height->setValue(CylinderShape->_height);
		m_originPoint->SetValue(CylinderShape->_position);
	}

	cacheActor = m_mainWindow->createCylinder(ui->radius->value(), ui->height->value());
	onPosChanged(0.f);
}

CreateCylinderWidget::~CreateCylinderWidget()
{
	delete ui;
}

void CreateCylinderWidget::onSizeChanged(double value)
{
	if (!cacheActor)
	{
		return;
	}
	// 这里可能不是value真的发生了变化
	cacheActor = m_mainWindow->UpdateCylinderActor(cacheActor, ui->radius->value(), ui->height->value());
	onPosChanged(0.f);
}

void CreateCylinderWidget::onPosChanged(double value)
{
	if (!cacheActor)
	{
		return;
	}
	cacheActor = m_mainWindow->UpdateActorPosition(cacheActor, gp_Pnt(ui->pX->value(), ui->pY->value(), ui->pZ->value()));
}

ShapeOperation* CreateCylinderWidget::GetOperation()
{
	ShapeOperation* op = new CreateShapeOperation(tr("圆柱体"), cacheActor->m_featureShape);
	return op;
}

void CreateCylinderWidget::GenerateFeatureShapeForCacheActor()
{
	cacheActor->m_featureShape.reset(new FeatureShapeCylinder(cacheActor->OccShape(), ui->radius->value(), ui->height->value()));
}

void CreateCylinderWidget::Reset()
{
	QRWCreateSolidWidget::Reset();

	ui->radius->setValue(0.5f);
	ui->height->setValue(1.f);
	m_originPoint->SetValue(gp_Pnt());

	cacheActor = m_mainWindow->createCylinder(ui->radius->value(), ui->height->value());
	onPosChanged(0.f);
}
